﻿namespace FiftyOne.Foundation.Mobile.Detection.Wurfl.Handlers
{
    using FiftyOne.Foundation.Mobile.Detection.Wurfl;
    using FiftyOne.Foundation.Mobile.Detection.Wurfl.Matchers;
    using System;

    internal class NokiaHandler : RegexSegmentHandler
    {
        private const string DEFAULT_DEVICE = "nokia_generic_series60";
        private static readonly string[] PATTERNS = new string[] { "(?:(?<=[Nokia|NOKIA])[^/]+)", @"(?<=(?:[Nokia|NOKIA][^/]+)/)[\d.]+", @"\([\d.]+\)" };

        public NokiaHandler() : base(PATTERNS, new int[] { 100, 1, 1 }, true)
        {
        }

        protected internal override bool CanHandle(string userAgent)
        {
            return (((userAgent.Contains("Nokia") || userAgent.Contains("Symbian OS")) || userAgent.Contains("NOKIA")) && base.CanHandle(userAgent));
        }

        protected internal override Results Match(string userAgent)
        {
            Results results = base.Match(userAgent);
            if (results == null)
            {
                DeviceInfo device = null;
                if (userAgent.Contains("Series60"))
                {
                    device = Provider.Instance.GetDeviceInfoFromID("nokia_generic_series60");
                }
                else if (userAgent.Contains("Series80"))
                {
                    device = Provider.Instance.GetDeviceInfoFromID("nokia_generic_series80");
                }
                if (device != null)
                {
                    results = new Results(device);
                }
            }
            return results;
        }

        internal override DeviceInfo DefaultDevice
        {
            get
            {
                DeviceInfo deviceInfoFromID = Provider.Instance.GetDeviceInfoFromID("nokia_generic_series60");
                if (deviceInfoFromID != null)
                {
                    return deviceInfoFromID;
                }
                return base.DefaultDevice;
            }
        }
    }
}

